import { ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/typings.d';
import type { RequestData } from '@ant-design/pro-table';
import { message } from 'antd';
import { request } from 'umi';
import { CreateLogisticsType, ListLogisticsType, UpdateLogisticsType } from './data';

/**
 * 获取配置列表
 * @param params
 */
export async function logisticsConfigPages(params: {
  page?: number;
  pageSize?: number;
  logisticsName?: string;
}): Promise<RequestData<ListLogisticsType>> {
  try {
    const res: ResponsePaginationData<ListLogisticsType> = await request(
      `/api/logistics/v1.0.0/config/list`,
      {
        params: {
          ...params,
          //   page: params.page,
          //   pageSize: params.pageSize,
        },
      },
    );
    if (!res || res.code !== SUCCESS_CODE) {
      message.error('获取失败');
      return { success: false, total: 0, data: [] };
    }

    return {
      success: true,
      total: res.data.total,
      data: res.data.list.map((item) => ({
        ...item,
      })),
    };
  } catch (error) {
    return { code: 1, message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员', data: [] };
  }
}
// 查询物流详情
export async function logisticsConfigDetail(
  id: string,
): Promise<{ success: boolean; data?: any; message: string; code: number }> {
  try {
    const res = await request(`/api/logistics/v1.0.0/config/list/${id}`, {});

    if (!res || res.code !== SUCCESS_CODE) {
      message.error('获取失败');
      return res;
    }
    return res;
  } catch (error) {
    return {
      success: false,
      data: undefined,
      message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员',
      code: 1,
    };
  }
}
// // 单个模板查询
// export async function staggerConfigOptions(
//   params: Pagination & {
//     templateName?: string;
//     tenant_id?: string;
//   },
// ): Promise<{ success: boolean; data?: any; message: string; code: number }> {
//   try {
//     const res: ResponsePaginationData<{
//       id: string;
//       name?: string;
//     }> = await request(`/peak/stagger/template/get_template_options`, {
//       params: {
//         tenant_id: params.tenant_id,
//         page: params.page || 1,
//         pageSize: params.pageSize || 10,
//         templateName: params.templateName,
//       },
//     });

//     if (!res || res.code !== SUCCESS_CODE) {
//       message.error('获取失败');
//       return { data: res.data.list, success: false, message: res.message, code: 1 };
//     }
//     return { data: res.data.list, success: true, message: res.message, code: 0 };
//   } catch (error) {
//     return {
//       success: false,
//       data: undefined,
//       message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员',
//       code: 1,
//     };
//   }
// }

// 添加物流配置
export async function addLogisticsConfig(
  params: CreateLogisticsType,
): Promise<{ success: boolean; message: string; code: number }> {
  try {
    const res = await request(`/api/logistics/v1.0.0/config/addList`, {
      method: 'POST',
      data: { ...params },
    });
    if (!res || res.code !== SUCCESS_CODE) {
      return res;
    }
    return res;
  } catch (error) {
    return {
      code: 1,
      success: false,
      message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员',
    };
  }
}

// 更新配置
export async function updateLogisticsConfig(
  params: UpdateLogisticsType,
): Promise<{ success: boolean; message: string; code: number }> {
  try {
    const res = await request(`/api/logistics/v1.0.0/config/updateList`, {
      method: 'POST',
      data: { ...params },
    });
    if (!res || res.code !== SUCCESS_CODE) {
      return res;
    }
    return res;
  } catch (error) {
    return {
      code: 1,
      success: false,
      message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员',
    };
  }
}

// 删除配置
export async function handleLogisticsConfig(id: string) {
  try {
    const res = await request(`/api/logistics/v1.0.0/config/delete/${id}`, {
      method: 'DELETE',
    });
    return res;
  } catch (error) {
    return { code: 1, message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员', data: [] };
  }
}

// 查询物流配置
export async function getLogisticsOptions(params: {
  page_size?: number;
  logisticsOption?: string;
  _id?: string;
}) {
  try {
    const res = await request('/api/logistics/v1.0.0/config/Options', {
      params: { ...params, page_size: 10 },
    });
    return res;
  } catch (error) {
    return { code: 1, message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员', data: [] };
  }
}
